package com.javabean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.model.DataBase;

public class AddDevice {
	PreparedStatement pre;
	Connection con = DataBase.getConnection();
	java.util.Date enddate;
	java.util.Date begindate;
	boolean flag = false;
	boolean isexist = false;
	ResultSet rs;
	Statement sta;
	String userName = "暂无描述";

	public String getUserName(long id) {
		userName = "暂无描述";
		String sql = "select UserName from user where UserId=?";
		try {
			pre = con.prepareStatement(sql);
			pre.setLong(1, id);
			rs = pre.executeQuery();
			while (rs.next()) {
				userName = rs.getString(1);

			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return userName;
	}

	private boolean isexist(String type) {
		String sql = "select * from device_type where type=?";

		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, type);
			rs = pre.executeQuery();
			if (rs.next()) {
				isexist = true;
			} else {
				isexist = false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return isexist;

	}

	public void AddType(String type, int amount, int available, String unit) {
		if (!isexist(type)) {
			String sql = "insert into device_type(type,amount,available, unit) values(?,?,?,?)";
			try {
				pre = con.prepareStatement(sql);
				pre.setString(1, type);
				pre.setInt(2, 1);
				pre.setInt(3, 1);
				pre.setString(4, "台");
				pre.executeUpdate();

			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
			addAmount(type);// 更新数量
		}
	}

	private void addAmount(String type) {
		String sql = "update device_type set amount=amount+1,available=available+1 where type=?";
		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, type);
			pre.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private int getCount() {
		String sql = "select max(did) from device";
		int count = 1;
		try {
			sta = con.createStatement();
			rs = sta.executeQuery(sql);
			while (rs.next()) {
				count = rs.getInt(1);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;

	}

	public void addRecord(long BorrowerId, String position) {

		int DeviceId = getCount();
		Date date = new Date();
		String sql = "insert into borrow_record(DeviceId,BorrowDate,BorrowerId,BorrowOrReturn,Position) values(?,?,?,?,?)";
		try {
			pre = con.prepareStatement(sql);
			pre.setInt(1, DeviceId);
			pre.setDate(2, new java.sql.Date(date.getTime()));
			pre.setLong(3, BorrowerId);
			pre.setString(4, "新入库");
			pre.setString(5, position);
			pre.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public boolean AddNew(String type, int serial, String model,
			String department, String producter, String edate, String mdate,
			String extracontent, String devuser) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			enddate = sdf.parse(edate);
			begindate = sdf.parse(mdate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String sql = "insert into device(type,model,department,serial,edate,mdate,vender,username,remark,isUse,state)values(?,?,?,?,?,?,?,?,?,?,?)";

		try {

			pre = con.prepareStatement(sql);
			pre.setString(1, type);
			pre.setString(2, model);
			pre.setString(3, department);
			pre.setInt(4, serial);
			pre.setDate(5, new java.sql.Date(enddate.getTime()));
			pre.setDate(6, new java.sql.Date(begindate.getTime()));
			pre.setString(7, producter);
			pre.setString(8, devuser);
			pre.setString(9, extracontent);
			pre.setString(10, "yes");
			pre.setString(11, "yes");
			pre.executeUpdate();
			flag = true;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return flag;

	}
	
	public boolean AddNew(String type, int serial, String model,
		String department, String producter, Date edate, Date mdate,
		String extracontent, String devuser) {

		enddate = edate;
		begindate = mdate;
		
		String sql = "insert into device(type,model,department,serial,edate,mdate,vender,username,remark,isUse,state)values(?,?,?,?,?,?,?,?,?,?,?)";

		try {

			pre = con.prepareStatement(sql);
			pre.setString(1, type);
			pre.setString(2, model);
			pre.setString(3, department);
			pre.setInt(4, serial);
			pre.setDate(5, new java.sql.Date(enddate.getTime()));
			pre.setDate(6, new java.sql.Date(begindate.getTime()));
			pre.setString(7, producter);
			pre.setString(8, devuser);
			pre.setString(9, extracontent);
			pre.setString(10, "yes");
			pre.setString(11, "yes");
			pre.executeUpdate();
			flag = true;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return flag;

	}
}
